
package net.swa.dewei.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.swa.dewei.beans.entity.DingDan;
import net.swa.dewei.beans.entity.FuwuZhan;
import net.swa.dewei.beans.entity.MenDian;
import net.swa.dewei.beans.entity.WeixinInfo;
import net.swa.dewei.beans.vo.VoMenDian;
import net.swa.dewei.service.MenDianService;
import net.swa.system.dao.HibernateDaoSupport;
import net.swa.util.BuzConst;
import net.swa.util.DateUtils;
import net.swa.util.JsonResult;
import net.swa.util.geography.GqueryUtil;
import net.swa.util.geography.VoSquare;
import net.swa.util.io.ExcelUtil;
import net.swa.util.io.enums.MsOfficeVersion;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Service;

@Service("menDianService")
public class MenDianServiceImpl extends HibernateDaoSupport implements MenDianService
{
    private final Logger log = Logger.getLogger(MenDianServiceImpl.class);

    /*
    @Override
    @SuppressWarnings("unchecked")
    public JsonResult<MenDian> queryByLatLng(final Double lat , final Double lng)
    {
        JsonResult<MenDian> json = new JsonResult<MenDian>();
        final VoSquare v = GqueryUtil.querySquarePoint(lng, lat, 20);
        //查询有效的门店
        String hql = "from MenDian where  status=" + BuzConst.STATUS_YES + " and longitude>=:minLng and longitude<=:maxLng and  latitude>=:minLat AND latitude<=:maxLat";
        Query query = getCurrentSession().createQuery(hql);
        query.setDouble("minLng", v.getMinLng());
        query.setDouble("maxLng", v.getMaxLng());
        query.setDouble("minLat", v.getMinLat());
        query.setDouble("maxLat", v.getMaxLat());
        List<MenDian> list = query.list();
        if (null != list && list.size() > 0)
        {
            // 求点最近的点（经纬度差的平方和最小的点）
            int minIndex = 0;
            double distance = calcfunction(lat, lng, list.get(0));
            for (int i = 0; i < list.size(); i++)
            {
                double dis = calcfunction(lat, lng, list.get(i));
                MenDian o = list.get(i);
                log.debug("门店：" + o.getName() + ",距离您" + dis + ",您的经纬度：" + lat + "," + lng + ";门店的经纬度：" + o.getLatitude() + "," + o.getLongitude());
                if (distance > dis)
                {
                    distance = dis;
                    minIndex = i;
                }
            }
            MenDian o = list.get(minIndex);
            java.text.DecimalFormat df = new java.text.DecimalFormat("0.0");
            o.setDistance(df.format(distance / 1000));
            List<MenDian> list2 = new ArrayList<MenDian>();
            o.setTuijian(true);
            list2.add(o);
            json.setResult(list2);
        }
        json.setSuccess(true);
        return json;
    }
    */
    // 求两点相聚距离
    private double calcfunction(double lat , double lng , MenDian menDian)
    {
        double EARTH_RADIUS = 6378.137;// 地球半径
        double radLat1 = rad(lat);
        double radLat2 = rad(menDian.getLatitude());
        double a = radLat1 - radLat2;
        double b = rad(lng) - rad(menDian.getLongitude());
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10;

        log.debug("两点间距离：" + s);
        return s;// Math.pow((menDian.getLatitude() - lat), 2) +
        // Math.pow(menDian.getLongitude() - lng, 2);
    }

    private double rad(double d)
    {
        return d * Math.PI / 180.0;
    }

    /*
    @Override
    public boolean add(MenDian model)
    {
        try
        {
            Query q = null;
            if (null != model.getWxid())
            {
                if (model.getChargeStation())
                {
                    model.setWxid(null);
                }
                else
                {
                    q = getCurrentSession().createQuery("update WeixinInfo set mendian=:mendian where id=:id ");
                    q.setBoolean("mendian", true);
                    q.setString("id", model.getWxid());
                    q.executeUpdate();

                }
                model.setCrateDate(DateUtils.getCurrDate(null));
                model.setStatus(BuzConst.STATUS_YES);
                getCurrentSession().save(model);
            }
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }
    */
    @Override
    public boolean delete(final Long[] ids)
    {
        for (int i = 0; i < ids.length; i++)
        {
            Query query = null;
            MenDian m = (MenDian) getCurrentSession().get(MenDian.class, ids[i]);
            if (null != m)
            {
                String wxid = m.getWxid();
                if (!StringUtils.isBlank(wxid))
                {
                    query = getCurrentSession().createQuery("update WeixinInfo set mendian=:mendian where id=:id");
                    query.setBoolean("mendian", false);
                    query.setString("id", wxid);
                    query.executeUpdate();
                }
            }
            query = getCurrentSession().createQuery("update MenDian set wxid=null, status=" + BuzConst.STATUS_NO + " where id=:id");
            query.setLong("id", ids[i]);
            query.executeUpdate();
        }
        return true;
    }

    /*
      @Override
      public boolean update(MenDian model)
      {
          model.setStatus(BuzConst.STATUS_YES);
          model.setUpdateDate(DateUtils.getCurrDate(null));
          if (model.getChargeStation())
          {
              model.setWxid(null);
          }
          getCurrentSession().update(model);
          Query q = getCurrentSession().createQuery("update WeixinInfo set mendian=:mendian where id=:id ");
          if (null != model.getWxid())
          {
              q.setBoolean("mendian", true);
              q.setString("id", model.getWxid());
              q.executeUpdate();
          }
          return true;
      }
      */

    /** 当更新前后的微信号不相等时候，将原来的微信号转为非门店用户 */
    @Override
    public boolean update(MenDian model , String oldWxid)
    {
        Query q = null;
        model.setStatus(BuzConst.STATUS_YES);
        model.setUpdateDate(DateUtils.getCurrDate(null));
        try
        {
            //若是纯充电站不该有微信信息
            //            if (model.getChargeStation())
            //            {
            //                model.setWxid(null);
            //            }
            getCurrentSession().update(model);
            q = getCurrentSession().createQuery("update WeixinInfo set mendian=:mendian where id=:id ");
            if (!StringUtils.isBlank(oldWxid))
            {
                q.setBoolean("mendian", false);
                q.setString("id", oldWxid);
                q.executeUpdate();
            }
            if (null != model.getWxid())
            {
                q.setBoolean("mendian", true);
                q.setString("id", model.getWxid());
                q.executeUpdate();
            }
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    /*
    @SuppressWarnings("unchecked")
    @Override
    public JsonResult<MenDian> queryMendianPage(final String name , final String nickname , Long fuwuzhanId , final String fuwuzhanName , Boolean ischarge , final int currentPage , final int pageSize , final String orderBy , final String orderType)
    {
        if (log.isDebugEnabled())
        {
            log.debug("传入参数 nickname is" + nickname + " ,fuwuzhanName is " + fuwuzhanName + ", currentPage is " + currentPage + ",pageSize is " + pageSize + ",orderBy is " + orderBy);
        }
        JsonResult<MenDian> json = new JsonResult<MenDian>();
        StringBuilder sql = new StringBuilder();
        StringBuilder countsql = new StringBuilder();
        Query query = null;
        countsql.append("select count(1) as counNum  from  t_MenDian m left join t_FuwuZhan f on f.id=m.fuwuzhanId left join t_WeixinInfo  w on w.openid=m.wxid  where 1=1 ");
        sql.append("select m.address, m.city, m.crateDate, m.detailInfo, m.fuwuzhanId, f.name as fuwuzhanName, m.id, m.imageUrl, m.latitude, m.longitude, m.lxr, m.lxrTel, m.name, w.nickname, m.province, m.town, m.updateDate, m.wxid,m.chargeStation,m.chargImageUrl,m.weixiu,m.xiaoshou,m.mendianNo from  t_MenDian m left join t_FuwuZhan f on f.id=m.fuwuzhanId  left join t_WeixinInfo   w  on w.openid=m.wxid   where 1=1 ");

        sql.append(" and m.status=" + BuzConst.STATUS_YES + "  ");
        countsql.append(" and m.status=" + BuzConst.STATUS_YES + "  ");

        if (null != ischarge)
        {
            sql.append(" and m.chargeStation=" + ischarge + "  ");
            countsql.append(" and m.chargeStation=" + ischarge + "  ");
        }
        if (!StringUtils.isBlank(nickname))
        {
            sql.append(" and w.nickname  like :nickname");
            countsql.append(" and w.nickname  like :nickname");
        }
        if (null != fuwuzhanId)
        {
            sql.append(" and m.fuwuzhanId= :fuwuzhanId ");
            countsql.append(" and m.fuwuzhanId= :fuwuzhanId ");
        }
        if (!StringUtils.isBlank(name))
        {
            sql.append(" and m.name  like :name");
            countsql.append(" and m.name  like :name");
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            sql.append(" and f.name like :fuwuzhanName");
            countsql.append(" and f.name like :fuwuzhanName");
        }
        if (!StringUtils.isBlank(orderBy))
        {
            sql.append("order by " + orderBy);
            if (!StringUtils.isBlank(orderType))
            {
                sql.append("  " + orderType);
            }
        }
        query = getCurrentSession().createSQLQuery(sql.toString()).addScalar("id", StandardBasicTypes.LONG).addScalar("address", StandardBasicTypes.STRING).addScalar("city", StandardBasicTypes.STRING).addScalar("crateDate", StandardBasicTypes.STRING).addScalar("detailInfo", StandardBasicTypes.STRING).addScalar("fuwuzhanId", StandardBasicTypes.LONG).addScalar("fuwuzhanName", StandardBasicTypes.STRING).addScalar("imageUrl", StandardBasicTypes.STRING).addScalar("latitude", StandardBasicTypes.DOUBLE).addScalar(
                "longitude", StandardBasicTypes.DOUBLE).addScalar("lxr", StandardBasicTypes.STRING).addScalar("lxrTel", StandardBasicTypes.STRING).addScalar("name", StandardBasicTypes.STRING).addScalar("nickname", StandardBasicTypes.STRING).addScalar("province", StandardBasicTypes.STRING).addScalar("town", StandardBasicTypes.STRING).addScalar("updateDate", StandardBasicTypes.STRING).addScalar("wxid", StandardBasicTypes.STRING).addScalar("chargeStation", StandardBasicTypes.BOOLEAN).addScalar("chargImageUrl",
                StandardBasicTypes.STRING).addScalar("weixiu", StandardBasicTypes.BOOLEAN).addScalar("xiaoshou", StandardBasicTypes.BOOLEAN).addScalar("mendianNo", StandardBasicTypes.STRING).setResultTransformer(Transformers.aliasToBean(MenDian.class));

        if (!StringUtils.isBlank(nickname))
        {
            query.setString("nickname", "%" + nickname + "%");
        }
        if (null != fuwuzhanId)
        {
            query.setLong("fuwuzhanId", fuwuzhanId);
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            query.setString("fuwuzhanName", "%" + fuwuzhanName + "%");
        }
        if (!StringUtils.isBlank(name))
        {
            query.setString("name", "%" + name + "%");
        }

        if (pageSize > 0)
        {
            int from = (currentPage - 1) * pageSize;
            query.setFirstResult(from);
            query.setMaxResults(pageSize);
        }
        List<MenDian> list = query.list();
        query = getCurrentSession().createSQLQuery(countsql.toString()).addScalar("counNum", StandardBasicTypes.INTEGER);
        if (!StringUtils.isBlank(nickname))
        {
            query.setString("nickname", "%" + nickname + "%");
        }
        if (null != fuwuzhanId)
        {
            query.setLong("fuwuzhanId", fuwuzhanId);
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            query.setString("fuwuzhanName", "%" + fuwuzhanName + "%");
        }
        if (!StringUtils.isBlank(name))
        {
            query.setString("name", "%" + name + "%");
        }
        if (pageSize > 0)
        {
            int totalCount = (Integer) query.uniqueResult();
            int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
            json.setTotalSize(totalCount);
            json.setTotalPage(totalPage);
            json.setPageSize(pageSize);
            json.setCurrentPage(currentPage);
        }
        json.setResult(list);
        return json;
    }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<MenDian> queryMendianByLatLng(Double lat , Double lng , Integer d , Boolean ischarge)
    {
        if (null == d)
        {
            d = 20;
        }
        final VoSquare v = GqueryUtil.querySquarePoint(lng, lat, 20);
        //查询有效的门店
        String hql = "from MenDian where  status=" + BuzConst.STATUS_YES + " and longitude>=:minLng and longitude<=:maxLng and  latitude>=:minLat AND latitude<=:maxLat ";
        if (ischarge != null)
        {
            hql += " and chargeStation= " + ischarge;
        }
        Query query = getCurrentSession().createQuery(hql);
        query.setDouble("minLng", v.getMinLng());
        query.setDouble("maxLng", v.getMaxLng());
        query.setDouble("minLat", v.getMinLat());
        query.setDouble("maxLat", v.getMaxLat());
        List<MenDian> list = query.list();
        List<MenDian> list2 = null;
        if (null != list && list.size() > 0)
        {
            for (int i = 0; i < list.size(); i++)
            {
                double dis = calcfunction(lat, lng, list.get(i));

                MenDian o = list.get(i);
                log.debug("门店：" + o.getName() + ",距离您" + dis + ",您的经纬度：" + lat + "," + lng + ";门店的经纬度：" + o.getLatitude() + "," + o.getLongitude());
                o.setDoubleDistance(dis);
                if (dis >= 1000)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0.0");
                    o.setDistance(df.format(dis / 1000) + "公里");
                }
                else if (dis > 100)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0");
                    o.setDistance(df.format(dis / 100) + "00米");
                }
                else
                {
                    o.setDistance("100米");
                }
            }

            Collections.sort(list);
            log.debug("查询结果如下：");
            list2 = new ArrayList<MenDian>();
            for (int i = 0; i < 5 && i < list.size(); i++)
            {
                if (i == 0)
                {
                    list.get(i).setTuijian(true);
                }
                else
                {
                    list.get(i).setTuijian(false);
                }
                log.debug("门店：" + list.get(i).getName() + ",\n距离您" + list.get(i).getDistance() + ",\n门店经纬度：" + list.get(i).getLatitude() + "," + list.get(i).getLongitude());
                list2.add(list.get(i));
            }
        }
        return list2;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MenDian> queryWxMendianByLatLng(Double lat , Double lng , Integer d , Boolean iswexiu)
    {
        if (null == d)
        {
            d = 20;
        }
        final VoSquare v = GqueryUtil.querySquarePoint(lng, lat, 20);
        //查询有效的门店
        String hql = "from MenDian where  status=" + BuzConst.STATUS_YES + " and longitude>=:minLng and longitude<=:maxLng and  latitude>=:minLat AND latitude<=:maxLat ";
        if (iswexiu != null)
        {
            hql += " and weixiu= " + iswexiu;
        }
        Query query = getCurrentSession().createQuery(hql);
        query.setDouble("minLng", v.getMinLng());
        query.setDouble("maxLng", v.getMaxLng());
        query.setDouble("minLat", v.getMinLat());
        query.setDouble("maxLat", v.getMaxLat());
        List<MenDian> list = query.list();
        List<MenDian> list2 = null;
        if (null != list && list.size() > 0)
        {
            for (int i = 0; i < list.size(); i++)
            {
                double dis = calcfunction(lat, lng, list.get(i));

                MenDian o = list.get(i);
                log.debug("门店：" + o.getName() + ",距离您" + dis + ",您的经纬度：" + lat + "," + lng + ";门店的经纬度：" + o.getLatitude() + "," + o.getLongitude());
                o.setDoubleDistance(dis);
                if (dis >= 1000)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0.0");
                    o.setDistance(df.format(dis / 1000) + "公里");
                }
                else if (dis > 100)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0");
                    o.setDistance(df.format(dis / 100) + "00米");
                }
                else
                {
                    o.setDistance("100米");
                }
            }

            Collections.sort(list);
            log.debug("查询结果如下：");
            list2 = new ArrayList<MenDian>();
            for (int i = 0; i < 5 && i < list.size(); i++)
            {
                if (i == 0)
                {
                    list.get(i).setTuijian(true);
                }
                else
                {
                    list.get(i).setTuijian(false);
                }
                log.debug("门店：" + list.get(i).getName() + ",\n距离您" + list.get(i).getDistance() + ",\n门店经纬度：" + list.get(i).getLatitude() + "," + list.get(i).getLongitude());
                list2.add(list.get(i));
            }
        }
        return list2;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MenDian> queryXsMendianByLatLng(Double lat , Double lng , Integer d , Boolean xiaoshou)
    {
        if (null == d)
        {
            d = 20;
        }
        final VoSquare v = GqueryUtil.querySquarePoint(lng, lat, 20);
        //查询有效的门店
        String hql = "from MenDian where  status=" + BuzConst.STATUS_YES + " and longitude>=:minLng and longitude<=:maxLng and  latitude>=:minLat AND latitude<=:maxLat ";
        if (xiaoshou != null)
        {
            hql += " and xiaoshou= " + xiaoshou;
        }
        Query query = getCurrentSession().createQuery(hql);
        query.setDouble("minLng", v.getMinLng());
        query.setDouble("maxLng", v.getMaxLng());
        query.setDouble("minLat", v.getMinLat());
        query.setDouble("maxLat", v.getMaxLat());
        List<MenDian> list = query.list();
        List<MenDian> list2 = null;
        if (null != list && list.size() > 0)
        {
            for (int i = 0; i < list.size(); i++)
            {
                double dis = calcfunction(lat, lng, list.get(i));

                MenDian o = list.get(i);
                log.debug("门店：" + o.getName() + ",距离您" + dis + ",您的经纬度：" + lat + "," + lng + ";门店的经纬度：" + o.getLatitude() + "," + o.getLongitude());
                o.setDoubleDistance(dis);
                if (dis >= 1000)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0.0");
                    o.setDistance(df.format(dis / 1000) + "公里");
                }
                else if (dis > 100)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0");
                    o.setDistance(df.format(dis / 100) + "00米");
                }
                else
                {
                    o.setDistance("100米");
                }
            }

            Collections.sort(list);
            log.debug("查询结果如下：");
            list2 = new ArrayList<MenDian>();
            for (int i = 0; i < 5 && i < list.size(); i++)
            {
                if (i == 0)
                {
                    list.get(i).setTuijian(true);
                }
                else
                {
                    list.get(i).setTuijian(false);
                }
                log.debug("门店：" + list.get(i).getName() + ",\n距离您" + list.get(i).getDistance() + ",\n门店经纬度：" + list.get(i).getLatitude() + "," + list.get(i).getLongitude());
                list2.add(list.get(i));
            }
        }
        return list2;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MenDian> queryMobiChargeByLatLng(Double lat , Double lng , Integer d , Boolean mobiCharge)
    {
        if (null == d)
        {
            d = 20;
        }
        final VoSquare v = GqueryUtil.querySquarePoint(lng, lat, 20);
        //查询有效的门店
        String hql = "from MenDian where  status=" + BuzConst.STATUS_YES + " and longitude>=:minLng and longitude<=:maxLng and  latitude>=:minLat AND latitude<=:maxLat ";
        if (mobiCharge != null)
        {
            hql += " and mobiCharge= " + mobiCharge;
        }
        Query query = getCurrentSession().createQuery(hql);
        query.setDouble("minLng", v.getMinLng());
        query.setDouble("maxLng", v.getMaxLng());
        query.setDouble("minLat", v.getMinLat());
        query.setDouble("maxLat", v.getMaxLat());
        List<MenDian> list = query.list();
        List<MenDian> list2 = null;
        if (null != list && list.size() > 0)
        {
            for (int i = 0; i < list.size(); i++)
            {
                double dis = calcfunction(lat, lng, list.get(i));

                MenDian o = list.get(i);
                log.debug("门店：" + o.getName() + ",距离您" + dis + ",您的经纬度：" + lat + "," + lng + ";门店的经纬度：" + o.getLatitude() + "," + o.getLongitude());
                o.setDoubleDistance(dis);
                if (dis >= 1000)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0.0");
                    o.setDistance(df.format(dis / 1000) + "公里");
                }
                else if (dis > 100)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0");
                    o.setDistance(df.format(dis / 100) + "00米");
                }
                else
                {
                    o.setDistance("100米");
                }
            }

            Collections.sort(list);
            log.debug("查询结果如下：");
            list2 = new ArrayList<MenDian>();
            for (int i = 0; i < 5 && i < list.size(); i++)
            {
                if (i == 0)
                {
                    list.get(i).setTuijian(true);
                }
                else
                {
                    list.get(i).setTuijian(false);
                }
                log.debug("门店：" + list.get(i).getName() + ",\n距离您" + list.get(i).getDistance() + ",\n门店经纬度：" + list.get(i).getLatitude() + "," + list.get(i).getLongitude());
                list2.add(list.get(i));
            }
        }
        return list2;
    }

    @SuppressWarnings("unchecked")
    @Override
    public JsonResult<MenDian> queryMendianPageList(final String name , final String nickname , Long fuwuzhanId , final String fuwuzhanName , String address , String lxr , final Boolean ischarge , final int currentPage , final int pageSize , final String orderBy , final String orderType)
    {
        if (log.isDebugEnabled())
        {
            log.debug("传入参数 nickname is" + nickname + " ,fuwuzhanName is " + fuwuzhanName + ", currentPage is " + currentPage + ",pageSize is " + pageSize + ",orderBy is " + orderBy);
        }
        JsonResult<MenDian> json = new JsonResult<MenDian>();
        StringBuilder sql = new StringBuilder();
        StringBuilder countsql = new StringBuilder();
        Query query = null;
        countsql.append("select count(1) as counNum  from  t_MenDian m join t_FuwuZhan f on f.id=m.fuwuzhanId left join t_WeixinInfo  w on w.openid=m.wxid  where 1=1 ");
        sql.append("select m.address, m.city, m.crateDate, m.detailInfo, m.fuwuzhanId, f.name as fuwuzhanName, m.id, m.imageUrl, m.latitude, m.longitude, m.lxr, m.lxrTel, m.name, w.nickname, m.province, m.town, m.updateDate, m.wxid,m.chargeStation,m.chargImageUrl,m.weixiu,m.xiaoshou,m.mendianNo from  t_MenDian m left join t_FuwuZhan f on f.id=m.fuwuzhanId  left join t_WeixinInfo   w  on w.openid=m.wxid   where 1=1 ");

        sql.append(" and m.status=" + BuzConst.STATUS_YES + "  ");
        countsql.append(" and m.status=" + BuzConst.STATUS_YES + "  ");

        if (null != ischarge)
        {
            sql.append(" and m.chargeStation=" + ischarge + "  ");
            countsql.append(" and m.chargeStation=" + ischarge + "  ");
        }

        if (!StringUtils.isBlank(nickname))
        {
            sql.append(" and w.nickname  like :nickname");
            countsql.append(" and w.nickname  like :nickname");
        }

        if (!StringUtils.isBlank(address))
        {
            sql.append(" and m.address  like :address");
            countsql.append(" and m.address  like :address");
        }
        if (!StringUtils.isBlank(lxr))
        {
            sql.append(" and m.lxr  like :lxr");
            countsql.append(" and m.lxr  like :lxr");
        }

        if (null != fuwuzhanId)
        {
            sql.append(" and m.fuwuzhanId= :fuwuzhanId ");
            countsql.append(" and m.fuwuzhanId= :fuwuzhanId ");
        }
        if (!StringUtils.isBlank(name))
        {
            sql.append(" and m.name  like :name");
            countsql.append(" and m.name  like :name");
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            sql.append(" and f.name like :fuwuzhanName");
            countsql.append(" and f.name like :fuwuzhanName");
        }
        if (!StringUtils.isBlank(orderBy))
        {
            sql.append("order by " + orderBy);
            if (!StringUtils.isBlank(orderType))
            {
                sql.append("  " + orderType);
            }
        }
        query = getCurrentSession().createSQLQuery(sql.toString()).addScalar("id", StandardBasicTypes.LONG).addScalar("address", StandardBasicTypes.STRING).addScalar("city", StandardBasicTypes.STRING).addScalar("crateDate", StandardBasicTypes.STRING).addScalar("detailInfo", StandardBasicTypes.STRING).addScalar("fuwuzhanId", StandardBasicTypes.LONG).addScalar("fuwuzhanName", StandardBasicTypes.STRING).addScalar("imageUrl", StandardBasicTypes.STRING).addScalar("latitude", StandardBasicTypes.DOUBLE).addScalar(
                "longitude", StandardBasicTypes.DOUBLE).addScalar("lxr", StandardBasicTypes.STRING).addScalar("lxrTel", StandardBasicTypes.STRING).addScalar("name", StandardBasicTypes.STRING).addScalar("nickname", StandardBasicTypes.STRING).addScalar("province", StandardBasicTypes.STRING).addScalar("town", StandardBasicTypes.STRING).addScalar("updateDate", StandardBasicTypes.STRING).addScalar("wxid", StandardBasicTypes.STRING).addScalar("weixiu", StandardBasicTypes.BOOLEAN).addScalar("xiaoshou",
                StandardBasicTypes.BOOLEAN).addScalar("mendianNo", StandardBasicTypes.STRING).addScalar("chargeStation", StandardBasicTypes.BOOLEAN).addScalar("chargImageUrl", StandardBasicTypes.STRING).setResultTransformer(Transformers.aliasToBean(MenDian.class));

        if (!StringUtils.isBlank(nickname))
        {
            query.setString("nickname", "%" + nickname + "%");
        }
        if (!StringUtils.isBlank(address))
        {
            query.setString("address", "%" + address + "%");
        }
        if (!StringUtils.isBlank(lxr))
        {
            query.setString("lxr", "%" + lxr + "%");
        }
        if (null != fuwuzhanId)
        {
            query.setLong("fuwuzhanId", fuwuzhanId);
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            query.setString("fuwuzhanName", "%" + fuwuzhanName + "%");
        }
        if (!StringUtils.isBlank(name))
        {
            query.setString("name", "%" + name + "%");
        }

        if (pageSize > 0)
        {
            int from = (currentPage - 1) * pageSize;
            query.setFirstResult(from);
            query.setMaxResults(pageSize);
        }
        List<MenDian> list = query.list();
        query = getCurrentSession().createSQLQuery(countsql.toString()).addScalar("counNum", StandardBasicTypes.INTEGER);
        if (!StringUtils.isBlank(nickname))
        {
            query.setString("nickname", "%" + nickname + "%");
        }
        if (!StringUtils.isBlank(address))
        {
            query.setString("address", "%" + address + "%");
        }
        if (!StringUtils.isBlank(lxr))
        {
            query.setString("lxr", "%" + lxr + "%");
        }
        if (null != fuwuzhanId)
        {
            query.setLong("fuwuzhanId", fuwuzhanId);
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            query.setString("fuwuzhanName", "%" + fuwuzhanName + "%");
        }
        if (!StringUtils.isBlank(name))
        {
            query.setString("name", "%" + name + "%");
        }
        if (pageSize > 0)
        {
            int totalCount = (Integer) query.uniqueResult();
            int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
            json.setTotalSize(totalCount);
            json.setTotalPage(totalPage);
            json.setPageSize(pageSize);
            json.setCurrentPage(currentPage);
        }
        json.setResult(list);
        return json;
    }

    @SuppressWarnings("unchecked")
    @Override
    public JsonResult<String> addModel(MenDian model , boolean isadmin)
    {
        JsonResult<String> json = new JsonResult<String>();
        try
        {
            Session session = getCurrentSession();
            Query q = null;
            if (null != model.getFuwuzhanId() && (!isadmin))
            {
                if (!StringUtils.isBlank(model.getProvince()))
                {
                    String sql = "select count(*) as countNum  from t_FuwuZhan f join t_FuwuZhanArea a on f.id=a.fuwuzhanId where f.id=:id and ( (f.province like :province1 ) or (a.province like :province1))";
                    if (!StringUtils.isBlank(model.getCity()) && (!(model.getCity()).equals(model.getProvince())))
                    {
                        if (!StringUtils.isBlank(model.getTown()))
                        {
                            if ((model.getCity()).equals(model.getTown()))
                            {
                                sql = "select count(*) as countNum  from t_FuwuZhan f join t_FuwuZhanArea a on f.id=a.fuwuzhanId where f.id=:id and ( (f.province like :province1  and f.town like :town1 ) or (a.province like :province1  and a.town like :town1))";
                            }
                            else
                            {
                                sql = "select count(*) as countNum  from t_FuwuZhan f join t_FuwuZhanArea a on f.id=a.fuwuzhanId where f.id=:id and ( (f.province like :province1 and f.city like :city1 and f.town like :town1 ) or (a.province like :province1 and a.city like :city1 and a.town like :town1))";
                            }
                        }
                        else
                        {
                            sql = "select count(*) as countNum  from t_FuwuZhan f join t_FuwuZhanArea a on f.id=a.fuwuzhanId where f.id=:id and ( (f.province like :province1 and f.city like :city1) or (a.province like :province1 and a.city like :city1))";
                        }
                    }
                    else if (!StringUtils.isBlank(model.getTown()))
                    {
                        sql = "select count(*) as countNum  from t_FuwuZhan f join t_FuwuZhanArea a on f.id=a.fuwuzhanId where f.id=:id and ( (f.province like :province1  and f.town like :town1 ) or (a.province like :province1  and a.town like :town1))";
                    }

                    q = session.createSQLQuery(sql).addScalar("countNum", StandardBasicTypes.INTEGER);
                    q.setLong("id", model.getFuwuzhanId());
                    String province = model.getProvince();
                    q.setString("province1", province.substring(0, (province.length() - 1)) + "%");

                    if (!StringUtils.isBlank(model.getCity()) && (!(model.getCity()).equals(model.getProvince())))
                    {
                        if (!StringUtils.isBlank(model.getTown()))
                        {
                            if ((model.getCity()).equals(model.getTown()))
                            {
                                log.debug("特殊位置：" + model.getProvince() + "," + model.getCity() + "," + model.getTown());
                            }
                            else
                            {
                                log.debug("普通位置：" + model.getProvince() + "," + model.getCity() + "," + model.getTown());
                                q.setString("city1", model.getCity().substring(0, (model.getCity().length() - 1)) + "%");
                            }
                            q.setString("town1", model.getTown().substring(0, (model.getTown().length() - 1)) + "%");
                        }
                        else
                        {
                            log.debug("没有区的位置：" + model.getProvince() + "," + model.getCity() + "," + model.getTown());
                            q.setString("city1", model.getCity().substring(0, (model.getCity().length() - 1)) + "%");
                        }
                    }
                    else if (!StringUtils.isBlank(model.getTown()))
                    {
                        log.debug("没有市的位置：" + model.getProvince() + "," + model.getCity() + "," + model.getTown());
                        q.setString("town1", model.getTown().substring(0, (model.getTown().length() - 1)) + "%");
                    }
                    else
                    {
                        log.debug("只有省的位置：" + model.getProvince() + "," + model.getCity() + "," + model.getTown());
                    }
                    int totalCount = (Integer) q.uniqueResult();
                    if (totalCount < 1)
                    {
                        json.setSuccess(false);
                        json.setMessage("该区域不允许您开店！");
                        return json;
                    }
                }
                else
                {
                    json.setSuccess(false);
                    json.setMessage("请标注您开店的位置，确保包含省市信息");
                    return json;
                }
            }
            model.setCrateDate(DateUtils.getCurrDate(null));
            model.setStatus(BuzConst.STATUS_YES);
            //若是纯充电站不该有微信信息
            //            if (model.getChargeStation())
            //            {
            //                model.setWxid(null);
            //            }
            if (!StringUtils.isBlank(model.getWxid()))
            {
                //微信必须不是门店才可以指定
                q = session.createQuery("from WeixinInfo where mendian=:mendian and id=:id ");
                q.setBoolean("mendian", false);
                q.setString("id", model.getWxid());
                List l = q.list();
                if (null != l && l.size() > 0)
                {
                    WeixinInfo wx = (WeixinInfo) l.get(0);
                    wx.setMendian(true);
                    session.update(wx);
                }
                else
                {
                    json.setSuccess(false);
                    json.setMessage("该微信已经被他人指定请重新选择。");
                    return json;
                }
            }
            session.save(model);
            json.setSuccess(true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage(e.toString());
        }
        return json;
    }

    @SuppressWarnings("unchecked")
    @Override
    public JsonResult<String> updateModel(MenDian model , String oldWxid , boolean isadmin)
    {
        JsonResult<String> json = new JsonResult<String>();
        try
        {
            Session session = getCurrentSession();
            Query q = null;
            if (null != model.getFuwuzhanId() && (!isadmin))
            {
                if (!StringUtils.isBlank(model.getProvince()))
                {
                    String sql = "select count(*) as countNum  from t_FuwuZhan f join t_FuwuZhanArea a on f.id=a.fuwuzhanId where f.id=:id and ( (f.province like :province1 ) or (a.province like :province1))";
                    if (!StringUtils.isBlank(model.getCity()) && (!(model.getCity()).equals(model.getProvince())))
                    {
                        if (!StringUtils.isBlank(model.getTown()))
                        {
                            if ((model.getCity()).equals(model.getTown()))
                            {
                                sql = "select count(*) as countNum  from t_FuwuZhan f join t_FuwuZhanArea a on f.id=a.fuwuzhanId where f.id=:id and ( (f.province like :province1  and f.town like :town1 ) or (a.province like :province1  and a.town like :town1))";
                            }
                            else
                            {
                                sql = "select count(*) as countNum  from t_FuwuZhan f join t_FuwuZhanArea a on f.id=a.fuwuzhanId where f.id=:id and ( (f.province like :province1 and f.city like :city1 and f.town like :town1 ) or (a.province like :province1 and a.city like :city1 and a.town like :town1))";
                            }
                        }
                        else
                        {
                            sql = "select count(*) as countNum  from t_FuwuZhan f join t_FuwuZhanArea a on f.id=a.fuwuzhanId where f.id=:id and ( (f.province like :province1 and f.city like :city1) or (a.province like :province1 and a.city like :city1))";
                        }
                    }
                    else if (!StringUtils.isBlank(model.getTown()))
                    {
                        sql = "select count(*) as countNum  from t_FuwuZhan f join t_FuwuZhanArea a on f.id=a.fuwuzhanId where f.id=:id and ( (f.province like :province1  and f.town like :town1 ) or (a.province like :province1  and a.town like :town1))";
                    }

                    q = session.createSQLQuery(sql).addScalar("countNum", StandardBasicTypes.INTEGER);
                    q.setLong("id", model.getFuwuzhanId());
                    String province = model.getProvince();
                    q.setString("province1", province.substring(0, (province.length() - 1)) + "%");

                    if (!StringUtils.isBlank(model.getCity()) && (!(model.getCity()).equals(model.getProvince())))
                    {
                        if (!StringUtils.isBlank(model.getTown()))
                        {
                            if ((model.getCity()).equals(model.getTown()))
                            {
                                log.debug("特殊位置：" + model.getProvince() + "," + model.getCity() + "," + model.getTown());
                            }
                            else
                            {
                                log.debug("普通位置：" + model.getProvince() + "," + model.getCity() + "," + model.getTown());
                                q.setString("city1", model.getCity().substring(0, (model.getCity().length() - 1)) + "%");
                            }
                            q.setString("town1", model.getTown().substring(0, (model.getTown().length() - 1)) + "%");
                        }
                        else
                        {
                            log.debug("没有区的位置：" + model.getProvince() + "," + model.getCity() + "," + model.getTown());
                            q.setString("city1", model.getCity().substring(0, (model.getCity().length() - 1)) + "%");
                        }
                    }
                    else if (!StringUtils.isBlank(model.getTown()))
                    {
                        log.debug("没有市的位置：" + model.getProvince() + "," + model.getCity() + "," + model.getTown());
                        q.setString("town1", model.getTown().substring(0, (model.getTown().length() - 1)) + "%");
                    }
                    else
                    {
                        log.debug("只有省的位置：" + model.getProvince() + "," + model.getCity() + "," + model.getTown());
                    }

                    int totalCount = (Integer) q.uniqueResult();
                    if (totalCount < 1)
                    {
                        json.setSuccess(false);
                        json.setMessage("该区域不允许您开店！");
                        return json;
                    }
                }
                else
                {
                    json.setSuccess(false);
                    json.setMessage("请标注您开店的位置，确保包含省市信息");
                    return json;
                }
            }
            model.setStatus(BuzConst.STATUS_YES);
            model.setUpdateDate(DateUtils.getCurrDate(null));

            //若是纯充电站不该有微信信息
            //            if (model.getChargeStation())
            //            {
            //                model.setWxid(null);
            //            }
            if (!StringUtils.isBlank(model.getWxid()))
            {
                boolean f = true;
                if (!StringUtils.isBlank(oldWxid))
                {
                    if (model.getWxid().equals(oldWxid))
                    {
                        f = false;
                    }
                }
                if (f)
                {
                    //微信必须不是门店才可以指定
                    q = session.createQuery("from WeixinInfo where mendian=:mendian and id=:id ");
                    q.setBoolean("mendian", false);
                    q.setString("id", model.getWxid());
                    List l = q.list();
                    if (null != l && l.size() > 0)
                    {
                        WeixinInfo wx = (WeixinInfo) l.get(0);
                        wx.setMendian(true);
                        session.update(wx);
                        q = session.createQuery("update WeixinInfo set mendian=:mendian where id=:id ");
                        if (!StringUtils.isBlank(oldWxid))
                        {
                            q.setBoolean("mendian", false);
                            q.setString("id", oldWxid);
                            q.executeUpdate();
                        }
                    }
                    else
                    {
                        json.setSuccess(false);
                        json.setMessage("该微信已经被他人指定请重新选择。");
                        return json;
                    }
                }
            }
            //            if (model.getChargeStation())
            //            {
            //                if (!StringUtils.isBlank(oldWxid))
            //                {
            //                    q = session.createQuery("update WeixinInfo set mendian=:mendian where id=:id ");
            //                    q.setBoolean("mendian", false);
            //                    q.setString("id", oldWxid);
            //                    q.executeUpdate();
            //                }
            //            }
            session.update(model);
            json.setSuccess(true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage(e.toString());
        }
        return json;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MenDian> queryMendianByModelLatLng(DingDan model , int d)
    {
        if (0 == d)
        {
            d = 20;
        }
        double lng = model.getBlng();
        double lat = model.getBlat();
        final VoSquare v = GqueryUtil.querySquarePoint(lng, lat, 20);
        //查询有效的门店
        String hql = "from MenDian where  status=" + BuzConst.STATUS_YES + " and longitude>=:minLng and longitude<=:maxLng and  latitude>=:minLat AND latitude<=:maxLat and weixiu= " + true;
        if (!StringUtils.isBlank(model.getProvince()))
        {
            hql += " and province like :province ";
            if (!StringUtils.isBlank(model.getCity()))
            {
                if (!StringUtils.isBlank(model.getTown()))
                {
                    if ((model.getTown()).equals(model.getCity()))
                    {//如果市和区县名称一样则不判断市
                        //济源市
                    }
                    else
                    {
                        hql += " and city like :city ";
                    }
                    if ("市辖区".equals(model.getTown()))
                    {

                    }
                    else
                    {
                        hql += " and town like :town ";
                    }
                }
            }
        }
        Query query = getCurrentSession().createQuery(hql);
        query.setDouble("minLng", v.getMinLng());
        query.setDouble("maxLng", v.getMaxLng());
        query.setDouble("minLat", v.getMinLat());
        query.setDouble("maxLat", v.getMaxLat());
        if (!StringUtils.isBlank(model.getProvince()))
        {
            String province = model.getProvince();
            if (province.length() > 3)
            {
                query.setString("province", province.substring(0, 2) + "%");
            }
            else
            {
                query.setString("province", province.substring(0, province.length() - 1) + "%");
            }
            log.debug("province is " + province);
            if (!StringUtils.isBlank(model.getCity()))
            {
                String city = model.getCity();
                if (!StringUtils.isBlank(model.getTown()))
                {
                    if ((model.getTown()).equals(city))
                    {//如果市和区县名称一样则不判断市

                    }
                    else
                    {
                        query.setString("city", city.substring(0, city.length() - 1) + "%");
                    }
                    if ("市辖区".equals(model.getTown()))
                    {
                    }
                    else
                    {
                        String town = model.getTown();
                        query.setString("town", town.substring(0, town.length() - 1) + "%");
                        log.debug("province is " + province + ",city is " + city + ",town is " + town);
                    }
                }
            }
        }
        List<MenDian> list = query.list();
        List<MenDian> list2 = null;
        if (null != list && list.size() > 0)
        {
            for (int i = 0; i < list.size(); i++)
            {
                double dis = calcfunction(lat, lng, list.get(i));

                MenDian o = list.get(i);
                log.debug("门店：" + o.getName() + ",距离您" + dis + ",您的经纬度：" + lat + "," + lng + ";门店的经纬度：" + o.getLatitude() + "," + o.getLongitude());
                o.setDoubleDistance(dis);
                if (dis >= 1000)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0.0");
                    o.setDistance(df.format(dis / 1000) + "公里");
                }
                else if (dis > 100)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0");
                    o.setDistance(df.format(dis / 100) + "00米");
                }
                else
                {
                    o.setDistance("100米");
                }
            }

            Collections.sort(list);
            log.debug("查询结果如下：");
            list2 = new ArrayList<MenDian>();
            for (int i = 0; i < 5 && i < list.size(); i++)
            {
                if (i == 0)
                {
                    list.get(i).setTuijian(true);
                }
                else
                {
                    list.get(i).setTuijian(false);
                }
                log.debug("门店：" + list.get(i).getName() + ",\n距离您" + list.get(i).getDistance() + ",\n门店经纬度：" + list.get(i).getLatitude() + "," + list.get(i).getLongitude());
                list2.add(list.get(i));
            }
        }
        return list2;
    }

    @SuppressWarnings("unchecked")
    @Override
    public JsonResult<MenDian> queryExtMendianPage(String name , String nickname , Long fuwuzhanId , String fuwuzhanName , Boolean ischarge , Boolean weixiu , Boolean xiaoshou , Boolean mobiCharge , int currentPage , int pageSize , String orderBy , String orderType)
    {
        if (log.isDebugEnabled())
        {
            log.debug("传入参数 nickname is" + nickname + " ,fuwuzhanName is " + fuwuzhanName + ", currentPage is " + currentPage + ",pageSize is " + pageSize + ",orderBy is " + orderBy);
        }
        JsonResult<MenDian> json = new JsonResult<MenDian>();
        StringBuilder sql = new StringBuilder();
        StringBuilder countsql = new StringBuilder();
        Query query = null;
        countsql.append("select count(1) as counNum  from  t_MenDian m left join t_FuwuZhan f on f.id=m.fuwuzhanId left join t_WeixinInfo  w on w.openid=m.wxid  where 1=1 ");
        sql.append("select m.address, m.city, m.crateDate, m.detailInfo, m.fuwuzhanId, f.name as fuwuzhanName, m.id, m.imageUrl, m.latitude, m.longitude, m.lxr, m.lxrTel, m.name, w.nickname, m.province, m.town, m.updateDate, m.wxid,m.chargeStation,m.chargImageUrl,m.weixiu,m.xiaoshou,m.mobiCharge,m.mendianNo from  t_MenDian m left join t_FuwuZhan f on f.id=m.fuwuzhanId  left join t_WeixinInfo   w  on w.openid=m.wxid   where 1=1 ");

        sql.append(" and m.status=" + BuzConst.STATUS_YES + " and(1=0  ");
        countsql.append(" and m.status=" + BuzConst.STATUS_YES + " and (1=0 ");

        if ((null == ischarge || (!ischarge)) && (null == xiaoshou || (!xiaoshou)) && (null == weixiu || (!weixiu)) && (null == mobiCharge || (!mobiCharge)))
        {
            sql.append(" or 1=1 ");
            countsql.append(" or 1=1 ");
        }
        else
        {
            if (null != ischarge && ischarge)
            {
                sql.append(" or m.chargeStation=" + ischarge + "  ");
                countsql.append(" or m.chargeStation=" + ischarge + "  ");
            }
            if (null != weixiu && weixiu)
            {
                sql.append(" or m.weixiu=" + weixiu + "  ");
                countsql.append(" or m.weixiu=" + weixiu + "  ");
            }
            if (null != xiaoshou && xiaoshou)
            {
                sql.append(" or m.xiaoshou=" + xiaoshou + "  ");
                countsql.append(" or m.xiaoshou=" + xiaoshou + "  ");
            }
            if (null != mobiCharge && mobiCharge)
            {
                sql.append(" or m.mobiCharge=" + mobiCharge + "  ");
                countsql.append(" or m.mobiCharge=" + mobiCharge + "  ");
            }
        }
        sql.append(" ) ");
        countsql.append(" )  ");

        if (!StringUtils.isBlank(nickname))
        {
            sql.append(" and w.nickname  like :nickname");
            countsql.append(" and w.nickname  like :nickname");
        }
        if (null != fuwuzhanId)
        {
            sql.append(" and m.fuwuzhanId= :fuwuzhanId ");
            countsql.append(" and m.fuwuzhanId= :fuwuzhanId ");
        }
        if (!StringUtils.isBlank(name))
        {
            sql.append(" and m.name  like :name");
            countsql.append(" and m.name  like :name");
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            sql.append(" and f.name like :fuwuzhanName");
            countsql.append(" and f.name like :fuwuzhanName");
        }
        if (!StringUtils.isBlank(orderBy))
        {
            sql.append("order by " + orderBy);
            if (!StringUtils.isBlank(orderType))
            {
                sql.append("  " + orderType);
            }
        }
        query = getCurrentSession().createSQLQuery(sql.toString()).addScalar("id", StandardBasicTypes.LONG).addScalar("address", StandardBasicTypes.STRING).addScalar("city", StandardBasicTypes.STRING).addScalar("crateDate", StandardBasicTypes.STRING).addScalar("detailInfo", StandardBasicTypes.STRING).addScalar("fuwuzhanId", StandardBasicTypes.LONG).addScalar("fuwuzhanName", StandardBasicTypes.STRING).addScalar("imageUrl", StandardBasicTypes.STRING).addScalar("latitude", StandardBasicTypes.DOUBLE).addScalar(
                "longitude", StandardBasicTypes.DOUBLE).addScalar("lxr", StandardBasicTypes.STRING).addScalar("lxrTel", StandardBasicTypes.STRING).addScalar("name", StandardBasicTypes.STRING).addScalar("nickname", StandardBasicTypes.STRING).addScalar("province", StandardBasicTypes.STRING).addScalar("town", StandardBasicTypes.STRING).addScalar("updateDate", StandardBasicTypes.STRING).addScalar("wxid", StandardBasicTypes.STRING).addScalar("chargeStation", StandardBasicTypes.BOOLEAN).addScalar("chargImageUrl",
                StandardBasicTypes.STRING).addScalar("weixiu", StandardBasicTypes.BOOLEAN).addScalar("xiaoshou", StandardBasicTypes.BOOLEAN).addScalar("mobiCharge", StandardBasicTypes.BOOLEAN)

        .addScalar("mendianNo", StandardBasicTypes.STRING).setResultTransformer(Transformers.aliasToBean(MenDian.class));

        if (!StringUtils.isBlank(nickname))
        {
            query.setString("nickname", "%" + nickname + "%");
        }
        if (null != fuwuzhanId)
        {
            query.setLong("fuwuzhanId", fuwuzhanId);
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            query.setString("fuwuzhanName", "%" + fuwuzhanName + "%");
        }
        if (!StringUtils.isBlank(name))
        {
            query.setString("name", "%" + name + "%");
        }

        if (pageSize > 0)
        {
            int from = (currentPage - 1) * pageSize;
            query.setFirstResult(from);
            query.setMaxResults(pageSize);
        }
        List<MenDian> list = query.list();
        query = getCurrentSession().createSQLQuery(countsql.toString()).addScalar("counNum", StandardBasicTypes.INTEGER);
        if (!StringUtils.isBlank(nickname))
        {
            query.setString("nickname", "%" + nickname + "%");
        }
        if (null != fuwuzhanId)
        {
            query.setLong("fuwuzhanId", fuwuzhanId);
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            query.setString("fuwuzhanName", "%" + fuwuzhanName + "%");
        }
        if (!StringUtils.isBlank(name))
        {
            query.setString("name", "%" + name + "%");
        }
        if (pageSize > 0)
        {
            int totalCount = (Integer) query.uniqueResult();
            int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
            json.setTotalSize(totalCount);
            json.setTotalPage(totalPage);
            json.setPageSize(pageSize);
            json.setCurrentPage(currentPage);
        }
        json.setResult(list);
        return json;
    }

    @SuppressWarnings("unchecked")
    @Override
    public JsonResult<MenDian> queryExtMendianPageList(String name , String nickname , Long fuwuzhanId , String fuwuzhanName , String address , String lxr , Boolean ischarge , Boolean weixiu , Boolean xiaoshou , Boolean mobiCharge , int currentPage , int pageSize , String orderBy , String orderType)
    {
        if (log.isDebugEnabled())
        {
            log.debug("传入参数 nickname is" + nickname + " ,fuwuzhanName is " + fuwuzhanName + ", currentPage is " + currentPage + ",pageSize is " + pageSize + ",orderBy is " + orderBy);
        }
        JsonResult<MenDian> json = new JsonResult<MenDian>();
        StringBuilder sql = new StringBuilder();
        StringBuilder countsql = new StringBuilder();
        Query query = null;
        countsql.append("select count(1) as counNum  from  t_MenDian m join t_FuwuZhan f on f.id=m.fuwuzhanId left join t_WeixinInfo  w on w.openid=m.wxid  where 1=1 ");
        sql.append("select m.address, m.city, m.crateDate, m.detailInfo, m.fuwuzhanId, f.name as fuwuzhanName, m.id, m.imageUrl, m.latitude, m.longitude, m.lxr, m.lxrTel, m.name, w.nickname, m.province, m.town, m.updateDate, m.wxid,m.chargeStation,m.chargImageUrl,m.weixiu,m.xiaoshou,m.mobiCharge,m.mendianNo from  t_MenDian m left join t_FuwuZhan f on f.id=m.fuwuzhanId  left join t_WeixinInfo   w  on w.openid=m.wxid   where 1=1 ");

        sql.append(" and m.status=" + BuzConst.STATUS_YES + " and(1=0  ");
        countsql.append(" and m.status=" + BuzConst.STATUS_YES + " and (1=0 ");
        if ((null == ischarge || (!ischarge)) && (null == xiaoshou || (!xiaoshou)) && (null == weixiu || (!weixiu)))
        {
            sql.append(" or 1=1 ");
            countsql.append(" or 1=1 ");
        }
        else
        {
            if (null != ischarge && ischarge)
            {
                sql.append(" or m.chargeStation=" + ischarge + "  ");
                countsql.append(" or m.chargeStation=" + ischarge + "  ");
            }
            if (null != weixiu && weixiu)
            {
                sql.append(" or m.weixiu=" + weixiu + "  ");
                countsql.append(" or m.weixiu=" + weixiu + "  ");
            }
            if (null != xiaoshou && xiaoshou)
            {
                sql.append(" or m.xiaoshou=" + xiaoshou + "  ");
                countsql.append(" or m.xiaoshou=" + xiaoshou + "  ");
            }
            if (null != mobiCharge && mobiCharge)
            {
                sql.append(" or m.mobiCharge=" + mobiCharge + "  ");
                countsql.append(" or m.mobiCharge=" + mobiCharge + "  ");
            }
        }
        sql.append(" ) ");
        countsql.append(" ) ");

        if (!StringUtils.isBlank(nickname))
        {
            sql.append(" and w.nickname  like :nickname");
            countsql.append(" and w.nickname  like :nickname");
        }

        if (!StringUtils.isBlank(address))
        {
            sql.append(" and m.address  like :address");
            countsql.append(" and m.address  like :address");
        }
        if (!StringUtils.isBlank(lxr))
        {
            sql.append(" and m.lxr  like :lxr");
            countsql.append(" and m.lxr  like :lxr");
        }

        if (null != fuwuzhanId)
        {
            sql.append(" and m.fuwuzhanId= :fuwuzhanId ");
            countsql.append(" and m.fuwuzhanId= :fuwuzhanId ");
        }
        if (!StringUtils.isBlank(name))
        {
            sql.append(" and m.name  like :name");
            countsql.append(" and m.name  like :name");
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            sql.append(" and f.name like :fuwuzhanName");
            countsql.append(" and f.name like :fuwuzhanName");
        }
        if (!StringUtils.isBlank(orderBy))
        {
            sql.append("order by " + orderBy);
            if (!StringUtils.isBlank(orderType))
            {
                sql.append("  " + orderType);
            }
        }
        query = getCurrentSession().createSQLQuery(sql.toString()).addScalar("id", StandardBasicTypes.LONG).addScalar("address", StandardBasicTypes.STRING).addScalar("city", StandardBasicTypes.STRING).addScalar("crateDate", StandardBasicTypes.STRING).addScalar("detailInfo", StandardBasicTypes.STRING).addScalar("fuwuzhanId", StandardBasicTypes.LONG).addScalar("fuwuzhanName", StandardBasicTypes.STRING).addScalar("imageUrl", StandardBasicTypes.STRING).addScalar("latitude", StandardBasicTypes.DOUBLE).addScalar(
                "longitude", StandardBasicTypes.DOUBLE).addScalar("lxr", StandardBasicTypes.STRING).addScalar("lxrTel", StandardBasicTypes.STRING).addScalar("name", StandardBasicTypes.STRING).addScalar("nickname", StandardBasicTypes.STRING).addScalar("province", StandardBasicTypes.STRING).addScalar("town", StandardBasicTypes.STRING).addScalar("updateDate", StandardBasicTypes.STRING).addScalar("wxid", StandardBasicTypes.STRING).addScalar("weixiu", StandardBasicTypes.BOOLEAN).addScalar("xiaoshou",
                StandardBasicTypes.BOOLEAN).addScalar("mobiCharge", StandardBasicTypes.BOOLEAN).addScalar("mendianNo", StandardBasicTypes.STRING).addScalar("chargeStation", StandardBasicTypes.BOOLEAN).addScalar("chargImageUrl", StandardBasicTypes.STRING).setResultTransformer(Transformers.aliasToBean(MenDian.class));

        if (!StringUtils.isBlank(nickname))
        {
            query.setString("nickname", "%" + nickname + "%");
        }
        if (!StringUtils.isBlank(address))
        {
            query.setString("address", "%" + address + "%");
        }
        if (!StringUtils.isBlank(lxr))
        {
            query.setString("lxr", "%" + lxr + "%");
        }
        if (null != fuwuzhanId)
        {
            query.setLong("fuwuzhanId", fuwuzhanId);
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            query.setString("fuwuzhanName", "%" + fuwuzhanName + "%");
        }
        if (!StringUtils.isBlank(name))
        {
            query.setString("name", "%" + name + "%");
        }

        if (pageSize > 0)
        {
            int from = (currentPage - 1) * pageSize;
            query.setFirstResult(from);
            query.setMaxResults(pageSize);
        }
        List<MenDian> list = query.list();
        query = getCurrentSession().createSQLQuery(countsql.toString()).addScalar("counNum", StandardBasicTypes.INTEGER);
        if (!StringUtils.isBlank(nickname))
        {
            query.setString("nickname", "%" + nickname + "%");
        }
        if (!StringUtils.isBlank(address))
        {
            query.setString("address", "%" + address + "%");
        }
        if (!StringUtils.isBlank(lxr))
        {
            query.setString("lxr", "%" + lxr + "%");
        }
        if (null != fuwuzhanId)
        {
            query.setLong("fuwuzhanId", fuwuzhanId);
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            query.setString("fuwuzhanName", "%" + fuwuzhanName + "%");
        }
        if (!StringUtils.isBlank(name))
        {
            query.setString("name", "%" + name + "%");
        }
        if (pageSize > 0)
        {
            int totalCount = (Integer) query.uniqueResult();
            int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
            json.setTotalSize(totalCount);
            json.setTotalPage(totalPage);
            json.setPageSize(pageSize);
            json.setCurrentPage(currentPage);
        }
        json.setResult(list);
        return json;
    }

    @SuppressWarnings("unchecked")
    @Override
    public HSSFWorkbook exportExcel(String name , String nickname , Long fuwuzhanId , String fuwuzhanName , String address , String lxr , Boolean ischarge , Boolean weixiu , Boolean xiaoshou , Boolean mobiCharge)
    {
        if (log.isDebugEnabled())
        {
            log.debug("传入参数 nickname is" + nickname + " ,fuwuzhanName is " + fuwuzhanName + ",address:" + address + ",lxr:" + lxr + "ischarge is :" + ischarge + "weixiu :" + weixiu + ",xiaoshou:" + xiaoshou);
        }
        StringBuilder sql = new StringBuilder();
        Query query = null;
        sql.append("select m.address, m.city, m.crateDate, m.detailInfo, m.fuwuzhanId, f.name as fuwuzhanName, m.id, m.imageUrl, m.latitude, m.longitude, m.lxr, m.lxrTel, m.name, w.nickname, m.province, m.town, m.updateDate, m.wxid,m.chargeStation,m.chargImageUrl,m.weixiu,m.xiaoshou,m.mobiCharge,m.mendianNo from  t_MenDian m left join t_FuwuZhan f on f.id=m.fuwuzhanId  left join t_WeixinInfo   w  on w.openid=m.wxid   where 1=1 ");

        sql.append(" and m.status=" + BuzConst.STATUS_YES + " and(1=0  ");

        if ((null == ischarge || (!ischarge)) && (null == xiaoshou || (!xiaoshou)) && (null == weixiu || (!weixiu)))
        {
            sql.append(" or 1=1 ");
        }
        else
        {
            if (null != ischarge && ischarge)
            {
                sql.append(" or m.chargeStation=" + ischarge + "  ");
            }
            if (null != weixiu && weixiu)
            {
                sql.append(" or m.weixiu=" + weixiu + "  ");
            }
            if (null != xiaoshou && xiaoshou)
            {
                sql.append(" or m.xiaoshou=" + xiaoshou + "  ");
            }
            if (null != mobiCharge && mobiCharge)
            {
                sql.append(" or m.mobiCharge=" + mobiCharge + "  ");
            }
        }
        sql.append(" ) ");

        if (!StringUtils.isBlank(nickname))
        {
            sql.append(" and w.nickname  like :nickname");
        }
        if (null != fuwuzhanId)
        {
            sql.append(" and m.fuwuzhanId= :fuwuzhanId ");
        }
        if (!StringUtils.isBlank(name))
        {
            sql.append(" and m.name  like :name");
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            sql.append(" and f.name like :fuwuzhanName");
        }

        query = getCurrentSession().createSQLQuery(sql.toString()).addScalar("id", StandardBasicTypes.LONG).addScalar("address", StandardBasicTypes.STRING).addScalar("city", StandardBasicTypes.STRING).addScalar("crateDate", StandardBasicTypes.STRING).addScalar("detailInfo", StandardBasicTypes.STRING).addScalar("fuwuzhanId", StandardBasicTypes.LONG).addScalar("fuwuzhanName", StandardBasicTypes.STRING).addScalar("imageUrl", StandardBasicTypes.STRING).addScalar("latitude", StandardBasicTypes.DOUBLE).addScalar(
                "longitude", StandardBasicTypes.DOUBLE).addScalar("lxr", StandardBasicTypes.STRING).addScalar("lxrTel", StandardBasicTypes.STRING).addScalar("name", StandardBasicTypes.STRING).addScalar("nickname", StandardBasicTypes.STRING).addScalar("province", StandardBasicTypes.STRING).addScalar("town", StandardBasicTypes.STRING).addScalar("updateDate", StandardBasicTypes.STRING).addScalar("wxid", StandardBasicTypes.STRING).addScalar("chargeStation", StandardBasicTypes.BOOLEAN).addScalar("chargImageUrl",
                StandardBasicTypes.STRING).addScalar("weixiu", StandardBasicTypes.BOOLEAN).addScalar("xiaoshou", StandardBasicTypes.BOOLEAN).addScalar("mobiCharge", StandardBasicTypes.BOOLEAN)

        .addScalar("mendianNo", StandardBasicTypes.STRING).setResultTransformer(Transformers.aliasToBean(MenDian.class));

        if (!StringUtils.isBlank(nickname))
        {
            query.setString("nickname", "%" + nickname + "%");
        }
        if (null != fuwuzhanId)
        {
            query.setLong("fuwuzhanId", fuwuzhanId);
        }
        if (!StringUtils.isBlank(fuwuzhanName))
        {
            query.setString("fuwuzhanName", "%" + fuwuzhanName + "%");
        }
        if (!StringUtils.isBlank(name))
        {
            query.setString("name", "%" + name + "%");
        }
        query.setMaxResults(BuzConst.MAX_RESULT);
        List<MenDian> list = query.list();
        HSSFWorkbook book = null;
        if (null != list && list.size() > 0)
        {
            // 要从对象中获取的属性名
            String fieldNames[] = { "id", "name", "mendianNo", "province", "city", "town", "address", "chargeStation", "weixiu", "xiaoshou", "mobiCharge", "latitude", "longitude", "lxr", "lxrTel", "crateDate", "updateDate", "fuwuzhanId", "fuwuzhanName", "imageUrl", "wxid", "nickname", "detailInfo" };
            String titleNames[] = { "序号", "门店名称", "门店编号", "省", "市", "县", "地址", "有充电站服务", "有维修服务", "有整车销售服务", "有手机充电服务", "经度", "纬度", "联系人", "联系电话", "创建时间", "更新时间", "服务站编号", "服务站名称", "门店图片地址", "微信openId", "微信昵称", "详细信息" };

            try
            {
                book = ExcelUtil.writeToExcelFile(list, fieldNames, titleNames, 0, 0, "filename", "sheetname", true, null, MsOfficeVersion.Office2003, null);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        return book;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> addExcel(String filePath , String errFilePath)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        String[][] data = new String[17][];
        try
        {
            data = ExcelUtil.readFromExcelFile(filePath, 1, 0, 17, "yyyy-MM-dd", "");
            if (null != data && data.length > BuzConst.MAX_RESULT)
            {
                map.put("success", false);
                map.put("message", "最多一次导入" + BuzConst.MAX_RESULT + "条记录");
                return map;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            log.debug("导入门店信息出现异常");
            map.put("success", false);
            map.put("message", "导入出现异常");
            return map;
        }
        map.put("success", false);
        map.put("message", "默认无数据导入失败");
        List<VoMenDian> errlist = new ArrayList<VoMenDian>();
        if (data.length > 1)
        {
            String[] tem = data[0];
            if (tem.length < 17)
            {
                map.put("message", "请下载模板，按照模板填写要要导入内容");
                return map;
            }
            String name = tem[0];
            String bianhao = tem[1];
            String province = tem[2];
            String city = tem[3];
            String town = tem[4];
            String address = tem[5];
            String charge = tem[6];
            String weixiu = tem[7];
            String xiaoshou = tem[8];
            String mobiCharge = tem[9];
            String lat = tem[10];
            String lng = tem[11];
            String lxr = tem[12];
            String lxrTel = tem[13];
            String fwzLoginName = tem[14];
            String url = tem[15];
            String detail = tem[16];
            Long fwzId = null;
            Double dlat = null;
            Double dlng = null;
            if (StringUtils.isBlank(province) || StringUtils.isBlank(city) || StringUtils.isBlank(town) || StringUtils.isBlank(name) || StringUtils.isBlank(bianhao) || StringUtils.isBlank(weixiu) || StringUtils.isBlank(xiaoshou) || StringUtils.isBlank(mobiCharge) || StringUtils.isBlank(lat) || StringUtils.isBlank(lng) || StringUtils.isBlank(lxr) || StringUtils.isBlank(lxrTel) || StringUtils.isBlank(fwzLoginName) || StringUtils.isBlank(url) || StringUtils.isBlank(address) || StringUtils.isBlank(charge))
            {
                map.put("message", "请按照模板填写要要导入内容");
                return map;
            }
            else if ((province.contains("省")) && (city.contains("市")) && (town.contains("县")) && (name.contains("门店名称")) && (bianhao.contains("编号")) && (weixiu.contains("维修")) && (xiaoshou.contains("销售")) && (lat.contains("经度")) && (lng.contains("纬度")) && (lxr.contains("联系人")) && (lxrTel.contains("电话")) && (fwzLoginName.contains("服务站")) && (url.contains("门店图片")) && (address.contains("地址")) && (charge.contains("充电")) && (mobiCharge.contains("手机充电")))
            {
                Session session = getCurrentSession();
                Query query = null;
                String fwzCheckHql = "from FuwuZhan where loginName=:loginName";

                String pctCheckHql = " from District where name=:name and pname=:pname ";
                for (int i = 1; i < data.length && i <= BuzConst.MAX_RESULT; i++)//最大支持Const.MAX_RESULT个批量导入
                {
                    tem = data[i];

                    name = tem[0];
                    bianhao = tem[1];
                    province = tem[2];
                    city = tem[3];
                    town = tem[4];
                    address = tem[5];
                    charge = tem[6];
                    weixiu = tem[7];
                    xiaoshou = tem[8];
                    mobiCharge = tem[9];
                    lat = tem[10];
                    lng = tem[11];
                    lxr = tem[12];
                    lxrTel = tem[13];
                    fwzLoginName = tem[14];
                    url = tem[15];
                    detail = tem[16];

                    VoMenDian p = new VoMenDian();
                    p.setName(name);
                    p.setMendianNo(bianhao);
                    p.setProvince(province);
                    p.setCity(city);
                    p.setTown(town);
                    p.setAddress(address);
                    p.setChargeStation(charge);
                    p.setWeixiu(weixiu);
                    p.setXiaoshou(xiaoshou);
                    p.setMobiCharge(mobiCharge);
                    p.setLatitude(lat);
                    p.setLongitude(lng);
                    p.setLxr(lxr);
                    p.setLxrTel(lxrTel);
                    p.setFuwuzhanName(fwzLoginName);
                    p.setImageUrl(url);
                    p.setDetailInfo(detail);

                    if (StringUtils.isBlank(province) && StringUtils.isBlank(city) && StringUtils.isBlank(town) && StringUtils.isBlank(name) && StringUtils.isBlank(bianhao) && StringUtils.isBlank(weixiu) && StringUtils.isBlank(xiaoshou) && StringUtils.isBlank(lat) && StringUtils.isBlank(lng) && StringUtils.isBlank(lxr) && StringUtils.isBlank(lxrTel) && StringUtils.isBlank(fwzLoginName) && StringUtils.isBlank(url) && StringUtils.isBlank(address) && StringUtils.isBlank(charge))
                    {
                        //第一步校验非空
                        break;
                    }

                    if (StringUtils.isBlank(name) || StringUtils.isBlank(bianhao))
                    {
                        //第一步校验非空
                        p.setErrorMsg("必填数据不可以有空,请检查");
                        errlist.add(p);
                        continue;
                    }

                    //第2步校验价格合法
                    Pattern pattern1 = Pattern.compile("^([1-2][0-9]{0,2})(\\.\\d{1,8})?$");
                    if (!StringUtils.isBlank(lat))
                    {
                        Matcher matcher1 = pattern1.matcher(lat);
                        if (!matcher1.matches())
                        {
                            p.setErrorMsg("经度数据不合法");
                            errlist.add(p);
                            continue;
                        }
                        else
                        {

                            dlat = Double.parseDouble(lat);
                        }
                    }
                    if (!StringUtils.isBlank(lng))
                    {
                        Matcher matcher2 = pattern1.matcher(lng);
                        if (!matcher2.matches())
                        {
                            p.setErrorMsg("纬度数据不合法");
                            errlist.add(p);
                            continue;
                        }
                        else
                        {
                            dlng = Double.parseDouble(lng);
                        }
                    }
                    if (!StringUtils.isBlank(fwzLoginName))
                    {
                        //若服务站名称存在校验是否合法
                        //检测该地区价格是否已经设置了
                        query = session.createQuery(fwzCheckHql);
                        query.setString("loginName", fwzLoginName);
                        List<FuwuZhan> fl = query.list();
                        if (null != fl && fl.size() > 0)
                        {
                            fwzId = fl.get(0).getId();
                        }
                        else
                        {
                            p.setErrorMsg("该服务站用户不存在");
                            errlist.add(p);
                            continue;
                        }

                    }
                    else
                    {
                        //                        p.setErrorMsg("服务站登陆名称必须填写");
                        //                        errlist.add(p);
                        //                        continue;
                    }
                    query = session.createQuery(pctCheckHql);
                    query.setString("pname", province);
                    query.setString("name", city);
                    List c1 = query.list();
                    if (null == c1 || 0 == c1.size())
                    {
                        p.setErrorMsg("本网不存在该地址，请检查省市");
                        errlist.add(p);
                        continue;
                    }
                    query.setString("pname", city);
                    query.setString("name", town);
                    c1 = query.list();
                    if (null == c1 || 0 == c1.size())
                    {
                        p.setErrorMsg("本网不存在该地址，请检查市和区县");
                        errlist.add(p);
                        continue;
                    }
                    MenDian m = new MenDian();
                    m.setName(name);
                    m.setMendianNo(bianhao);
                    m.setProvince(province);
                    m.setCity(city);
                    m.setTown(town);
                    m.setAddress(address);
                    if (!StringUtils.isBlank(charge))
                    {
                        if (charge.contains("是"))
                        {
                            m.setChargeStation(true);
                        }
                    }
                    if (!StringUtils.isBlank(weixiu))
                    {
                        if (charge.contains("是"))
                        {
                            m.setWeixiu(true);
                        }
                    }
                    if (!StringUtils.isBlank(xiaoshou))
                    {
                        if (charge.contains("是"))
                        {
                            m.setXiaoshou(true);
                        }
                    }
                    if (!StringUtils.isBlank(mobiCharge))
                    {
                        if (charge.contains("是"))
                        {
                            m.setMobiCharge(true);
                        }
                    }
                    m.setLatitude(dlat);
                    m.setLongitude(dlng);
                    m.setLxr(lxr);
                    m.setLxrTel(lxrTel);
                    m.setFuwuzhanName(fwzLoginName);
                    m.setImageUrl(url);
                    m.setDetailInfo(detail);
                    m.setFuwuzhanId(fwzId);
                    m.setStatus(BuzConst.STATUS_YES);
                    m.setCrateDate(DateUtils.getCurrDate(null));
                    session.save(m);
                }
                map.put("success", true);
                map.put("message", "导入成功");
                if (errlist.size() > 0)
                {
                    map.put("message", "有数据不合法");
                    // 要从对象中获取的属性名  
                    String fieldNames[] = { "name", "mendianNo", "province", "city", "town", "address", "chargeStation", "weixiu", "xiaoshou", "mobiCharge", "latitude", "longitude", "lxr", "lxrTel", "fuwuzhanName", "imageUrl", "detailInfo", "errorMsg" };
                    String titleNames[] = { "门店名称", "门店编号", "省", "市", "县", "地址", "有充电站服务", "有维修服务", "有整车销售服务", "有手机充电服务", "经度", "纬度", "联系人", "联系电话", "服务站登陆名称", "门店图片地址", "详细信息", "错误信息" };

                    try
                    {
                        String fileName = System.currentTimeMillis() + "";
                        fileName = ExcelUtil.writeToExcelFileTemp(errlist, fieldNames, titleNames, 0, 0, errFilePath + "\\" + fileName, "sheetname", true, null, MsOfficeVersion.Office2003, null);
                        map.put("errorFile", fileName);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
            else
            {
                map.put("success", false);
                map.put("message", "请下载模板，按照模板填写要要导入内容");
                return map;
            }
        }
        else
        {
            map.put("success", false);
            map.put("message", "无数据导入,请按模板填写导入");
        }
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MenDian> queryMendianNearByLatLng(double lat , double lng , Boolean chargeStation , Boolean weixiu , Boolean xiaoshou , Boolean mobiCharge , Integer d)
    {
        if (null == d)
        {
            d = 20;
        }
        final VoSquare v = GqueryUtil.querySquarePoint(lng, lat, 20);
        StringBuilder sb = new StringBuilder();
        //查询有效的门店
        sb.append("from MenDian where  status=").append(BuzConst.STATUS_YES).append(" and longitude>=:minLng and longitude<=:maxLng and  latitude>=:minLat AND latitude<=:maxLat ");

        sb.append(" and(1=0 ");
        if (mobiCharge != null)
        {
            if (mobiCharge)
            {
                sb.append(" or mobiCharge= " + mobiCharge);
            }
        }
        if (weixiu != null)
        {
            if (weixiu)
            {
                sb.append(" or weixiu= " + weixiu);
            }
        }
        if (xiaoshou != null)
        {
            if (xiaoshou)
            {
                sb.append(" or xiaoshou= " + xiaoshou);
            }
        }
        if (chargeStation != null)
        {
            if (chargeStation)
            {
                sb.append(" or chargeStation= " + chargeStation);
            }
        }
        sb.append(" )");
        Query query = getCurrentSession().createQuery(sb.toString());
        query.setDouble("minLng", v.getMinLng());
        query.setDouble("maxLng", v.getMaxLng());
        query.setDouble("minLat", v.getMinLat());
        query.setDouble("maxLat", v.getMaxLat());
        List<MenDian> list = query.list();
        List<MenDian> list2 = null;
        if (null != list && list.size() > 0)
        {
            for (int i = 0; i < list.size(); i++)
            {
                double dis = calcfunction(lat, lng, list.get(i));

                MenDian o = list.get(i);
                log.debug("门店：" + o.getName() + ",距离您" + dis + ",您的经纬度：" + lat + "," + lng + ";门店的经纬度：" + o.getLatitude() + "," + o.getLongitude());
                o.setDoubleDistance(dis);
                if (dis >= 1000)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0.0");
                    o.setDistance(df.format(dis / 1000) + "公里");
                }
                else if (dis > 100)
                {
                    java.text.DecimalFormat df = new java.text.DecimalFormat("0");
                    o.setDistance(df.format(dis / 100) + "00米");
                }
                else
                {
                    o.setDistance("100米");
                }
            }

            Collections.sort(list);
            log.debug("查询结果如下：");
            list2 = new ArrayList<MenDian>();
            for (int i = 0; i < 5 && i < list.size(); i++)
            {
                if (i == 0)
                {
                    list.get(i).setTuijian(true);
                }
                else
                {
                    list.get(i).setTuijian(false);
                }
                log.debug("门店：" + list.get(i).getName() + ",\n距离您" + list.get(i).getDistance() + ",\n门店经纬度：" + list.get(i).getLatitude() + "," + list.get(i).getLongitude());
                list2.add(list.get(i));
            }
        }
        return list2;
    }
}